﻿using Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cake_Management.Models;
using System.Web.Services.Description;

namespace Cake_Management.Controllers
{
    public class HT_GoodsController : Controller
    {
        // GET: HT_Goods
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetAll()
        {
            var result = new BLL.B_Goods().GetAll();

            var ff = from a in result
                     select new
                     {
                         a.Id,
                         a.GoodsName,
                         Cover=$"<a href='{ConfigurationManager.AppSettings["ServerPath"]+a.Cover}'target='_blank' onmouseover='viewIn(this)'onmouseout='viewOut()'>蛋糕图片</a>",
                         a.GoodsHeat,
                         a.Material,
                         a.Packing,
                         a.Explan,
                         a.Gift,
                         a.Delivery,
                         CategoryId = a.Category.CategoryName,
                         TastId =a.Taste.TasteName,
                         LayerId=a.Layer.LayerName,
                         ShapeId=a.Shape.ShapeName,
                         aa = $"<div class='btn btn-warning data-Edit'data-id='{a.Id}'onclick='Edit(this)'>编辑</div>   <div class='btn btn-danger data-del' data-id='{a.Id}' onclick='Delete(this)'>删除</div>"
                     };
            var data = new { data = ff };
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.category = new BLL.B_Category().GetAll();
            ViewBag.layer=new BLL.B_Layer().GetAll();
            ViewBag.taste=new BLL.B_Taste().GetAll();
            ViewBag.shape=new BLL.B_Shape().GetAll();
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(Goods goods)
        {
            var a = new
            {
                Base64 = goods.Cover,
                Past = "Goods"
            };
            var result = HttpHelper.HttpPost<string>(ConfigurationManager.AppSettings["ServerPath"] + "api/Base64/Base64Code", "post", a);
            goods.Cover = result.Data;
            new BLL.B_Goods().Add(goods);
            return Json(new { Code=200,Message="添加成功"},JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            try
            {
                new BLL.B_Goods().Delete(id);
                return Json(new { Code = 200, Message = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new {Code=500,Message="请先删除当前商品下的属性"+ex.Message});
            }
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            ViewBag.category = new BLL.B_Category().GetAll();
            ViewBag.layer = new BLL.B_Layer().GetAll();
            ViewBag.taste = new BLL.B_Taste().GetAll();
            ViewBag.shape = new BLL.B_Shape().GetAll();
            var data = new BLL.B_Goods().FindById(id);
            return PartialView(data);
        }

        [HttpPost]
        public ActionResult Edit(Goods goods)
        {
            if (goods.Cover.Contains(","))
            {
                var a = new
                {
                    Base64 = goods.Cover,
                    Past = "Goods"
                };
                var result = HttpHelper.HttpPost<string>(ConfigurationManager.AppSettings["ServerPath"] + "api/Base64/Base64Code", "post", a);
                goods.Cover = result.Data;
                new BLL.B_Goods().Updata(goods.Id, goods);
                return Json(new { Code = 200, Message = "修改成功" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                new BLL.B_Goods().Updata(goods.Id, goods);
                return Json(new { Code = 200, Message = "修改成功" }, JsonRequestBehavior.AllowGet);
            }
           
        }

    }
}